#include "ui_dashboard.h"
#include "app_controller.h"


lv_obj_t * create_dashboard_screen(dashboard_widgets *widgets)
{
    LOG_TRACE();
    
    lv_obj_t *scr_dash = lv_obj_create(NULL);
    if (scr_dash == NULL)
    {
        perror("scr_dash error");
        return NULL;
    }

    LOG_TRACE();
    lv_obj_set_style_bg_color(scr_dash, lv_color_hex(0x0000CD), 0);
    LOG_TRACE();



    //创建仪表盘并给仪表盘设置标签
    widgets->cpu_meter = lv_meter_create(scr_dash);
    if(widgets->cpu_meter == NULL) {
        printf("FATAL: create_dashboard_screen() failed at lv_meter_create()!\n");
        return NULL; // 或者返回 scr_dash，让部分界面显示
    }  
    lv_obj_set_size(widgets->cpu_meter,280,280);
    lv_obj_align(widgets->cpu_meter,LV_ALIGN_TOP_LEFT,40,40);
    
    LOG_TRACE();

    //添加并且配置刻度尺
    lv_meter_scale_t *scale = lv_meter_add_scale(widgets->cpu_meter);
     if (scale == NULL) {
        
        printf("error: create_dashboard_screen() 失败\n");
        return scr_dash; // 返回已经创建的部分，至少不会崩溃
    }
    //设置刻度,对cpu_meter对象的scale刻度尺设置51个刻度，宽度为2，长度为10
    lv_meter_set_scale_ticks(widgets->cpu_meter,scale,51,2,10,lv_palette_main(LV_PALETTE_GREY));
    //设置显目的主刻度,最后的10代表每隔十个设置一个刻度
    lv_meter_set_scale_major_ticks(widgets->cpu_meter,scale,10,4,15,lv_color_hex(0xffffff),10);
    lv_meter_set_scale_range(widgets->cpu_meter,scale,0,100,240,150);
    LOG_TRACE();

    //添加背景颜色增强视觉效果
    //背景颜色的实现其实就是添加一个圆弧区域
    //蓝色代表安全区0% ~ 40%
    lv_meter_indicator_t *in_blue = lv_meter_add_arc(widgets->cpu_meter,scale,10,lv_palette_main(LV_PALETTE_BLUE),0);
    lv_meter_set_indicator_start_value(widgets->cpu_meter,in_blue,0);
    lv_meter_set_indicator_end_value(widgets->cpu_meter,in_blue,40);

        LOG_TRACE();

    //绿色代表正常区40% ~ 80%
    lv_meter_indicator_t *in_green = lv_meter_add_arc(widgets->cpu_meter,scale,10,lv_palette_main(LV_PALETTE_GREEN),0);
    lv_meter_set_indicator_start_value(widgets->cpu_meter,in_green,40);
    lv_meter_set_indicator_end_value(widgets->cpu_meter,in_green,80);
LOG_TRACE();

    //红色代表警告80% ~ 1000%
    lv_meter_indicator_t *in_red = lv_meter_add_arc(widgets->cpu_meter,scale,10,lv_palette_main(LV_PALETTE_RED),0);
    lv_meter_set_indicator_start_value(widgets->cpu_meter,in_red,80);
    lv_meter_set_indicator_end_value(widgets->cpu_meter,in_red,100);
LOG_TRACE();


    //添加仪表盘的指针
    //设置仪表盘控件lv_meter_add_needle_line设置指针式仪表盘
    /* lv_meter_indicator_t * indic = lv_meter_add_needle_line(
    meter,    // 仪表盘对象
    scale,    // 关联的刻度
    width,    // 指针宽度（像素）
    color,    // 指针颜色
    offset    // 偏移量（负值向内）
); */
   widgets->cpu_meter_indicator = lv_meter_add_needle_line(widgets->cpu_meter,scale,4,lv_palette_main(LV_PALETTE_GREY),-10);
LOG_TRACE();
   //在仪表盘中心创建显示百分比的标签
   widgets->cpu_meter_label = lv_label_create(widgets->cpu_meter);
   lv_label_set_text(widgets->cpu_meter_label,"0%");
   lv_obj_set_style_text_font(widgets->cpu_meter_label,&lv_font_montserrat_32,0);
   lv_obj_center(widgets->cpu_meter_label);
LOG_TRACE();

// 【新增】创建一个容器作为“卡片”
lv_obj_t *mem_card = lv_obj_create(scr_dash);
lv_obj_set_size(mem_card, 340, 150); // 给卡片一个尺寸
lv_obj_align(mem_card, LV_ALIGN_TOP_RIGHT, -20, 40);
// 设置卡片样式：半透明背景，带边框
lv_obj_set_style_bg_opa(mem_card, LV_OPA_50, 0);
lv_obj_set_style_border_color(mem_card, lv_palette_main(LV_PALETTE_GREY), 0);
lv_obj_set_style_border_width(mem_card, 1, 0);

// 【新增】在卡片里创建标题
lv_obj_t *mem_title = lv_label_create(mem_card);
lv_label_set_text(mem_title, "MEMORY STATUS");
lv_obj_align(mem_title, LV_ALIGN_TOP_MID, 0, 5);


   //创建内存区域


   widgets->mem_bar = lv_bar_create(mem_card);
   lv_obj_align(widgets->mem_bar, LV_ALIGN_CENTER, 0, 0); 
   lv_obj_set_size(widgets->mem_bar,300,25);
   lv_obj_set_style_bg_color(widgets->mem_bar, lv_color_hex(0x303030), LV_PART_MAIN);
   lv_obj_set_style_bg_opa(widgets->mem_bar, LV_OPA_COVER, LV_PART_MAIN);

   lv_obj_set_style_bg_color(widgets->mem_bar, lv_palette_main(LV_PALETTE_CYAN), LV_PART_INDICATOR);
   lv_obj_set_style_bg_opa(widgets->mem_bar, LV_OPA_COVER, LV_PART_INDICATOR);

   //创建标签
   widgets->mem_label = lv_label_create(mem_card);
  
   lv_label_set_text(widgets->mem_label,"RAM:");
   lv_obj_set_style_text_color(widgets->mem_label, lv_color_hex(0xFFFFFF), 0);
   lv_obj_set_style_text_font(widgets->mem_label, &lv_font_montserrat_18, 0);
   lv_obj_align_to(widgets->mem_label,widgets->mem_bar,LV_ALIGN_OUT_BOTTOM_MID,0,10);
LOG_TRACE();
   //创建CPU曲线图
   widgets->cpu_chart = lv_chart_create(scr_dash);
   if (widgets->cpu_chart == NULL)
   {
      perror("lv_chart_create error...");
      return NULL;
   }
   
   lv_obj_set_size(widgets->cpu_chart,944,200);
   lv_obj_align(widgets->cpu_chart,LV_ALIGN_BOTTOM_MID,0,-40);
   lv_chart_set_type(widgets->cpu_chart,LV_CHART_TYPE_LINE);
   //分别设置X轴代表120秒，每秒钟一个点Y轴代表cpu_usage
   lv_chart_set_range(widgets->cpu_chart,LV_CHART_AXIS_PRIMARY_Y,0,100);
   lv_chart_set_point_count(widgets->cpu_chart,120);
   lv_chart_set_update_mode(widgets->cpu_chart,LV_CHART_UPDATE_MODE_SHIFT);//将旧数据移动到左侧，并把新数据添加到右侧
   
   
   lv_obj_t * chart_label = lv_label_create(scr_dash);
   lv_label_set_text(chart_label,"CPU History_Chart");
   lv_obj_set_style_text_color(chart_label,lv_color_hex(0xFFFFFF), 0);
   lv_obj_set_style_text_font(chart_label,&lv_font_montserrat_32,0);
   lv_obj_align_to(chart_label,widgets->cpu_chart,LV_ALIGN_OUT_TOP_MID,0,-5);

   LOG_TRACE();
   //为图标添加数据线
   widgets->cpu_chart_series = lv_chart_add_series(widgets->cpu_chart,lv_palette_main(LV_PALETTE_CYAN),LV_CHART_AXIS_PRIMARY_Y);
LOG_TRACE();

   widgets->temp_label = lv_label_create(scr_dash);
   lv_label_set_text(widgets->temp_label, "Temperture: -- C");
   lv_obj_set_style_text_color(widgets->temp_label, lv_color_hex(0xFFFFFF), 0); // 设置为纯白色
   lv_obj_align(widgets->temp_label, LV_ALIGN_BOTTOM_LEFT, 40, -10);


   return scr_dash;
}